package com.bjc.base.web;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bjc.base.bean.ResultVO;
import com.bjc.base.bean.request.BookStockReqVO;
import com.bjc.base.bean.request.BorrowReturnLogReqVO;
import com.bjc.base.bean.response.BorrowReturnLogVO;
import com.bjc.base.core.ResultGenerator;
import com.bjc.base.service.IBookStockService;
import com.bjc.base.service.IBorrowReturnLogService;
import com.bjc.base.utils.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * <p>
 * 绘本归还日志 前端控制器
 * </p>
 *
 * @author 许自辉
 * @since 2018-06-10
 */
@RestController
@RequestMapping("api/book")
@Api(value = "borrowReturnLog-api", tags = { "绘本归还日志相关接口" })
public class BorrowReturnLogController {

	@Autowired
	private IBorrowReturnLogService borrowReturnLogService;
	@Autowired
	private IBookStockService bookStockService;

	@ApiOperation("分页查询绘本归还日志")
	@GetMapping("borrowReturnLog/findPage")
	public ResultVO getBorrowReturnLogPage(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize,
			BorrowReturnLogReqVO borrowReturnLogReqVO) {
		PageHelper.startPage(pageNum, pageSize);
		List<BorrowReturnLogVO> borrowReturnLogVOs = borrowReturnLogService.getList(borrowReturnLogReqVO);
		return ResultGenerator.getSuccess(new PageInfo<BorrowReturnLogVO>(borrowReturnLogVOs));
	}
	
	@ApiOperation("获取当前人分享绘本的借阅记录")
	@GetMapping("borrowReturnLog/getMyBook")
	public ResultVO getMyBook(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize,BorrowReturnLogReqVO reqVO) {
		PageHelper.startPage(pageNum, pageSize);
		List<BorrowReturnLogVO> borrowReturnLogVOs = borrowReturnLogService.getMyBook(reqVO);
		return ResultGenerator.getSuccess(new PageInfo<BorrowReturnLogVO>(borrowReturnLogVOs));
	}

	@ApiOperation("分页查询绘本归还日志")
	@GetMapping("borrowReturnLog/find")
	public ResultVO getBorrowReturnLoge(BorrowReturnLogReqVO borrowReturnLogReqVO) {
		BorrowReturnLogVO borrowReturnLogVO = borrowReturnLogService.get(borrowReturnLogReqVO);
		return ResultGenerator.getSuccess(borrowReturnLogVO);
	}

	@ApiOperation("获取借阅每个绘本种类的数量")
	@GetMapping("borrowReturnLog/bookCountByType")
	public ResultVO getBookCountByType(BorrowReturnLogReqVO borrowReturnLogReqVO) {
		List<BorrowReturnLogVO> borrowReturnLogVO = borrowReturnLogService.getBookCountByType(borrowReturnLogReqVO);
		return ResultGenerator.getSuccess(borrowReturnLogVO);
	}

	@ApiOperation("添加绘本归还日志")
	@PostMapping("borrowReturnLog/create")
	public ResultVO createBorrowReturnLog(@RequestBody BorrowReturnLogReqVO borrowReturnLogReqVOs) {
		int count = borrowReturnLogService.insert(borrowReturnLogReqVOs);
		return ResultGenerator.getInsertSuccess(count);
	}

	@ApiOperation("更新绘本归还日志")
	@PutMapping("borrowReturnLog/back")
	public ResultVO updateBorrowReturnLog(@RequestBody BorrowReturnLogReqVO borrowReturnLogReqVOs) {
		String ids = borrowReturnLogReqVOs.getIds();
		int count = 0;
		if (!StringUtil.isNullOrEmpty(ids)) {
			String[] idss = ids.split(",");
			for (int i = 0; i < idss.length; i++) {
				Integer id = Integer.parseInt(idss[i]);
				// 归还绘本
				borrowReturnLogReqVOs.setId(id);
				borrowReturnLogReqVOs.setReturnTime(LocalDateTime.now());
				borrowReturnLogService.update(borrowReturnLogReqVOs);

				// 如果归还，则将库存状态设置已归还
				if (2 == borrowReturnLogReqVOs.getState()) {
					BorrowReturnLogVO borrowReturnLogVO = borrowReturnLogService.getById(id);
					BookStockReqVO bookStockReqVO = new BookStockReqVO();
					bookStockReqVO.setId(borrowReturnLogVO.getBid());
					bookStockReqVO.setIsLock(0);
					bookStockService.update(bookStockReqVO);
				}

				count++;
			}
		} else {
			count = borrowReturnLogService.update(borrowReturnLogReqVOs);
		}
		return ResultGenerator.getUpdateSuccess(count);
	}

	@ApiOperation("批量删除绘本归还日志")
	@DeleteMapping("borrowReturnLog/deleteList")
	public ResultVO deleteBorrowReturnLog(@ApiParam(value = "borrowReturnLog id集合") @RequestBody int[] ids) {
		int count = borrowReturnLogService.deleteList(ids);
		return ResultGenerator.getDeleteSuccess(count);
	}


}
